package com.letv.app.ljodfh.ui.fragment;

import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AbsoluteLayout;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.jiongbull.jlog.JLog;
import com.letv.app.ljodfh.ui.adapter.HomeTVChannelAdapter;
import com.ruihang.generalibrary.ui.dialog.BaseDialogFragment;
import com.ruihang.generalibrary.ui.util.FloatVideoUtils2;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.ruihang.generalibrary.ui.widget.CustomEXImageView;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.ListUtils;
import com.ruihang.generalibrary.utils.NetModeUtil;
import com.ruihang.generalibrary.utils.UrlUtils;
import com.ruihang.ijkplaylib.widget.GPlayerView;
import com.letv.app.ljodfh.R;
import com.letv.app.ljodfh.base.Constances;
import com.letv.app.ljodfh.beans.FChannel;
import com.letv.app.ljodfh.beans.FProgram;
import com.letv.app.ljodfh.beans.TvLivingList;
import com.letv.app.ljodfh.http.AbsNetCallBack;
import com.letv.app.ljodfh.http.NetUtils;
import com.letv.app.ljodfh.ui.adapter.TvAdapter;
import com.letv.app.ljodfh.ui.dialog.NetModeDiaFragment;
import com.letv.app.ljodfh.ui.utils.GSFPlayerScreenControllImp;
import com.letv.app.ljodfh.ui.utils.GlideUtil;
import com.letv.app.ljodfh.ui.utils.OnFullClick;
import com.letv.app.ljodfh.ui.utils.SampleListener;
import com.letv.app.ljodfh.ui.utils.TVBottomControllerImp;
import com.letv.app.ljodfh.ui.utils.TVTopControllerImp;
import com.letv.app.ljodfh.ui.widget.BasePlayerView;
import com.shuyu.gsyvideoplayer.listener.INetPromt;
import com.shuyu.gsyvideoplayer.utils.OrientationUtils;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.letv.app.ljodfh.ui.dialog.NetModeDiaFragment.NO_TAG;
import static com.letv.app.ljodfh.ui.dialog.NetModeDiaFragment.OK_TAG;

/**
 * Created by Administrator on 2017/7/13 0013.
 */
public class TvFragment extends ColumnBaseFragment implements CanRefreshLayout.OnRefreshListener {
    @BindView(R.id.fragment_tv_img)
    protected CustomEXImageView img;
    //    @ViewInject(R.id.fragment_tv_title)
//    private CustomFontTextView tv_title;
    @BindView(R.id.fragment_tv_img_play)
    protected ImageView play;
    //    @BindView(R.id.fragment_tv_tabLayout)
//    protected TabLayout tabLayout;
    //    @BindView(R.id.title)
//    protected View popTitleView;
//    @BindView(R.id.tv_tv_name)
//    protected CustomFontTextView tvPopTitle;
    @BindView(R.id.tv_living_refresh)
    protected CanRefreshLayout refreshLayout;
    @BindView(R.id.can_content_view)
    protected RecyclerView recyclerView;

    @BindView(R.id.v_tv_shape)
    protected View vShape;

    //    @BindView(R.id.fragment_tv_viewPager)
//    protected ViewPager viewPager;
    //    @BindView(R.id.iv_tv_arrow_down)
//    protected ImageView vArrowDown;
    private TvAdapter tvAdapter;

    private String mProgramBackUrl;//该值为null代表尚未获取到url，为空串代表获取失败
    private boolean mIsLoading = false;
    private boolean mIsProgramBackUrlRequesting = false;
    private String mVideoUrl;
    /**
     * 卫视直播的url
     */
    private String liveUrl;

    private String liveImgUrl;
    //    private ArrayList<FChannel> channels;
    private FChannel currentFChannel;

    private FloatVideoUtils2 floatVideoUtilss;
    private boolean isPortrait = true;
    @BindView(R.id.video_container)
    protected AbsoluteLayout videoContainer;
    private Handler handler;
    private TVTopControllerImp topController;
    private GSFPlayerScreenControllImp screenController;
    private TVBottomControllerImp bottomControllerImp;
    private NetModeDiaFragment netDiaFragment;
    private String titleTemp;
    private BasePlayerView videoPlayer;
    /**
     * 直播的标题缓存
     */
    private String liveTitleTemp;

    private boolean isPlayingWhenPause;

    private boolean isFirst = true;//是否第一次播放

    private HomeTVChannelAdapter channelAdapter;

    private OrientationUtils orientationUtils2;

    private OnItemInternalClick itemInternalClick = new OnItemInternalClick() {
        @Override
        public void onItemInternalClick(View itemView, View view, int position) {
            FChannel tempChannel = channelAdapter.getItem(position);
            if (tempChannel == null && tempChannel.getChannelId().equals(currentFChannel.getChannelId())) {
//                typeSelectPopup.dismiss();
                return;
            }
            // 在这里获取item数据
//            String value = typedata.get(position);
            // 把选择的数据展示对应的TextView上
//            tvPopTitle.setText(value);
            currentFChannel = tempChannel;
            getTvLivingData(tempChannel);
//            if (ListUtils.isListValued(channels)) {
//            }
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tv, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    protected void initViews(View v, @Nullable Bundle savedInstanceState) {
//        tv_title.setText("获取视频数据中...");
        //为tablayout设置竖的分割线
//        final LinearLayout linearLayout = (LinearLayout) tabLayout.getChildAt(0);
//        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
//        linearLayout.setDividerDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.tab_divider_vertical_df));
//        linearLayout.setDividerPadding(getResources().getDimensionPixelOffset(R.dimen.tab_divider_ver_padding));
//        tvAdapter = new TvAdapter(getChildFragmentManager(), getActivity());
//        tvAdapter.setParentShowing(isThisShowing);
////        viewPager.setAdapter(tvAdapter);
////        viewPager.setOffscreenPageLimit(1);
////        tabLayout.setupWithViewPager(viewPager);
//        tabLayout.addOnTabSelectedListener(this);

        refreshLayout.setOnRefreshListener(this);
        handler = new Handler();
//        handler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                initVideoPlayer();
//            }
//        }, 500);


    }
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (floatVideoUtilss != null) {
            if (isParentShowing && !hidden) {
                floatVideoUtilss.onResume();
            } else {
                floatVideoUtilss.onPause();
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        isFront = true;
        if (floatVideoUtilss != null) {
            floatVideoUtilss.onResume();
        }
//        getData();
//        if (refreshLayout != null) {
//            refreshLayout.autoRefresh();
//        }
    }

    @Override
    public void onPause() {
        isFront = false;
        if (floatVideoUtilss != null) {
            floatVideoUtilss.onPause();
        }
        super.onPause();
    }

    @Override
    public void onRefresh() {
        if (mIsLoading) {
            return;
        }
        mIsLoading = true;
//        if (TextUtils.isEmpty(((TvFragment) getParentFragment()).getProgramBackUrl())) {
//            ((TvFragment) getParentFragment()).setLoadingState(true);
//        }
        getData();
    }

    @Override
    protected boolean onFragmentShow(boolean isFirst, boolean isCallFromResume) {
//        JLog.e("jjaj ddac  is first=" + isFirst + "  resume " + isCallFromResume + "   url=" + mVideoUrl + "   " + this);
        isFront = true;
        if (isFirst) {
//            if (floatVideoUtilss != null) {
//                if (videoPlayer != null && !TextUtils.isEmpty(videoPlayer.getOriUrl()) && NetModeUtil.GetNetype(getContext()) == 1) {
//                    videoPlayer.setMute(true);
//                    videoPlayer.startPlay();
//                }
//            }
            if (refreshLayout != null) {
                refreshLayout.autoRefresh();
            }
            if (!TextUtils.isEmpty(mVideoUrl)) {
                playVideo();
            } else {
                if (img == null || img.getHeight() == 0) {
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            initVideoPlayer();
                        }
                    }, 300);
                } else {
                    initVideoPlayer();
                }
            }
        } else {
            if (floatVideoUtilss != null) {
                if (isPlayingWhenPause) {
                    if(null!=currentFChannel) {
                        getTvLivingData(currentFChannel);
                    }
                    floatVideoUtilss.onResume();
                }
            } else {
                if (videoPlayer != null) {
                    videoPlayer.onVideoResume();
                }
            }
//            if (videoPlayer)
        }
        return super.onFragmentShow(isFirst, isCallFromResume);
    }

    @Override
    protected void onFragmentUnShowed(boolean isCallFromPause) {
        super.onFragmentUnShowed(isCallFromPause);
//        JLog.e("ddac  cal from pause  " + isCallFromPause + "  floatutil=" + floatVideoUtils);
        isFront = false;
        if (floatVideoUtilss != null) {
            if (floatVideoUtilss.isPlaying()) {
                JLog.e("ddac   will to pause");
                isPlayingWhenPause = true;
                floatVideoUtilss.onPause();
            }
        }
        if (videoPlayer != null) {
            if (videoPlayer.getCurrentState() == BasePlayerView.CURRENT_STATE_PLAYING) {
                isPlayingWhenPause = true;
            }
            videoPlayer.onVideoPause();
            videoPlayer.setOrientationChangeEnable(false);
        }
        if (orientationUtils2 != null) {
            orientationUtils2.setEnable(false);
        }
    }

    @Override
    public boolean hasShowPlayer() {
        return videoPlayer != null && videoPlayer.getVisibility() == View.VISIBLE;
    }

    //    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        if (tab.getPosition() == 0) {
            if (!TextUtils.isEmpty(liveUrl)) {
                if (!TextUtils.equals(liveUrl, mVideoUrl)) {
                    titleTemp = liveTitleTemp;
                    setVideoPlayer(liveUrl);
                }
            }
        }

    }

    //    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
    }

    //    @Override
    public void onTabReselected(TabLayout.Tab tab) {
    }

    @Override
    public void onDestroyView() {
//        if (tabLayout != null) {
//            tabLayout.removeOnTabSelectedListener(this);
//        }
        if (floatVideoUtilss != null) {
            floatVideoUtilss.onDestory();
        }
        if (orientationUtils2 != null) {
            orientationUtils2.releaseListener();
        }
        super.onDestroyView();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (tvAdapter != null) {
            tvAdapter.setParentShowing(isVisibleToUser);
        }
    }

    @OnClick(value = {R.id.fragment_tv_img_play})
    public void click(View view) {
        switch (view.getId()) {
            case R.id.fragment_tv_img_play:
                if (TextUtils.isEmpty(mVideoUrl)) {
                    toast("数据错误");
                    return;
                }
                playVideo();
                break;
//            case R.id.title:
//                initSelectPopup();
//                if (typeSelectPopup != null && !typeSelectPopup.isShowing()) {
//                    typeSelectPopup.showAsDropDown(popTitleView, 0, 0, Gravity.NO_GRAVITY);
////                    setBackgroundAlpha(0.5f);
//                    vShape.setVisibility(View.VISIBLE);
//                    vArrowDown.animate().setDuration(200).rotation(180).start();
//                }
//                break;
        }
    }


    private void getProgramBackData() {
        mIsProgramBackUrlRequesting = false;
//        if (viewPager.getCurrentItem() == 1) {
//            ((ProgramBackFragment) tvAdapter.getItem(1)).getFirstList();
//        }
//        else {
//            ((ProgramBackFragment) tvAdapter.getItem(1)).setIsLoading(false);
//        }
    }

    public void requestProgramBackUrl() {
        if (mIsLoading || mProgramBackUrl == null) {
            mIsProgramBackUrlRequesting = true;
        } else {
            getProgramBackData();
        }
    }

    public void setLoadingState(boolean isLoading) {
        mIsLoading = isLoading;
    }

    public String getProgramBackUrl() {
        return mProgramBackUrl;
    }

//    public void setData(String programBackUrl, String id, String title, String videoUrl) {
//        mProgramBackUrl = programBackUrl;
////        tv_title.setText(TextUtils.isEmpty(title) ? getString(R.string.default_title) : title);
//        JLog.e("dada=" + videoUrl);
//        liveUrl = videoUrl;
//        setVideoPlayer(videoUrl);
//        titleTemp = title;
//        liveTitleTemp = title;
//
//        if (topController != null) {
//            topController.setTitle(titleTemp);
//        }
//
//
//        if (mIsProgramBackUrlRequesting) {
//            getProgramBackData();
//        }
//    }

    public String getChannelUrl() {
        return channelUrl;
    }

    private boolean setVideoPlayer(String videoUrl) {
        mVideoUrl = videoUrl;
        // if (NetUtils.isWifiConnected(getContext())) {
        if (isFront) {
            playVideo();
            return true;
        }
        return false;
        //}
    }

    private void playVideo() {
        int netType = NetModeUtil.GetNetype(getContext());
        boolean haseCallstop = false;
        if (getContext() == null) {
            return;
        }
        if (initVideoPlayer()) {
//            bottomController.initInfo(liveInfo);
            if (orientationUtils2 == null) {
//                orientationUtils2 = new OrientationUtils(getActivity(), videoPlayer);
//                orientationUtils2.setRotateWithSystem(false);
            }
//            orientationUtils.setEnable(false);
            haseCallstop = true;
        } else {
//            if (TextUtils.equals(mVideoUrl, floatVideoUtils.getPlayerView().getCurrentUrl())) {
//                floatVideoUtils.stopVideoView();
//                haseCallstop = true;
//            }
            haseCallstop = false;
        }
        boolean isLiving = TextUtils.equals(mVideoUrl, liveUrl);
        videoPlayer.setLiving(isLiving);
        if (isLiving && videoPlayer.getThumbImge() != null) {
            GlideUtil.getGlideWithLarge169DefNotFit(getContext(), liveImgUrl).into(videoPlayer.getThumbImge());
        }
        videoPlayer.setUp(mVideoUrl, false, titleTemp);
        if (netType == 1) {
//            if (isFirst) {
//                videoPlayer.setMute(true);
//            }
            if (isFront) {
                videoPlayer.startPlay();
            }
//            if (haseCallstop) {
//                handler.postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
////                        floatVideoUtils.setLiving(TextUtils.equals(mVideoUrl, liveUrl)).play(img, mVideoUrl);
//                        videoPlayer.startPlayLogic();
//                    }
//                }, 700);
//            } else {
//                floatVideoUtils.setLiving(TextUtils.equals(mVideoUrl, liveUrl)).play(img, mVideoUrl);
//                videoPlayer.setUp(mVideoUrl, false, titleTemp);
//                if (topController != null) {
//                    topController.setTitle(titleTemp);
//                }
//            }
        }
        isFirst = false;

    }

    private void showNetProt() {
        if (netDiaFragment == null) {
            netDiaFragment = new NetModeDiaFragment();
        }
        netDiaFragment.setCallBack(netBack);
        netDiaFragment.show(getChildFragmentManager(), NET_TAG);
    }

    private synchronized boolean initVideoPlayer() {
        JLog.e("jjaj   c=" + getContext());
        if (getContext() == null) {
            return false;
        }
        JLog.e("jjaj    img h=" + img.getHeight());
        if (floatVideoUtilss == null) {
            floatVideoUtilss = new FloatVideoUtils2(getContext(), img, videoContainer, 0, true);
            if (videoPlayer == null) {
                videoPlayer = new BasePlayerView(getContext());
                JLog.e("vvav  tv player has created");
                videoPlayer.setLiving(true);
                videoPlayer.setNetPromt(new INetPromt() {
                    @Override
                    public boolean onNetPromt() {
                        showNetProt();
                        return false;
                    }
                });
                videoPlayer.setFullClick(new OnFullClick() {
                    @Override
                    public void onFullClick(StandardGSYVideoPlayer player, boolean isLandscape) {
                        JLog.e("rrsr player=" + player + "   islandscape=" + isLandscape);
                        if (isLandscape) {
                            if (orientationUtils2 != null) {
                                orientationUtils2.resolveByClick();
                                orientationUtils2.backToProtVideo();
                            } else {
                                actionToPortrait();
                            }
                            StandardGSYVideoPlayer.backFromWindowFull(getContext());
                        } else {
                            if (orientationUtils2 != null) {
                                orientationUtils2.resolveByClick();
                            } else {
                                actionToLandscape();
                            }
                            //第一个true是否需要隐藏actionbar，第二个true是否需要隐藏statusbar
                            player.startWindowFullscreen(getContext(), false, false);
                        }
                    }
                });
                videoPlayer.setStandardVideoAllCallBack(new SampleListener() {
                    @Override
                    public void onPrepared(String url, Object... objects) {
                        super.onPrepared(url, objects);
                        if (orientationUtils2 != null) {
                            orientationUtils2.setEnable(true);
                        }
                    }

                    @Override
                    public void onQuitFullscreen(String url, Object... objects) {
                        super.onQuitFullscreen(url, objects);
//                        JLog.e("qqaq  state=" + videoPlayer.getCurrentState() + "    " + objects[1]);
                        if (orientationUtils2 != null) {
                            JLog.e("ttat  " + orientationUtils2.isEnable() + "   " + orientationUtils2.isRotateWithSystem() + "   " + videoPlayer.getOrientationUtils());
                        }
                    }
                });

                floatVideoUtilss.setPlayerView(videoPlayer);
            }
            floatVideoUtilss.showPlayer(img, false);

            return true;
        }
        return false;
    }

    private void onChangeToPlaying() {
    }

    private void onChangeToStop() {
    }

    private void onPlayerConstructor(GPlayerView playerView, ViewGroup base) {
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (floatVideoUtilss != null) {
            isPortrait = newConfig.orientation == Configuration.ORIENTATION_PORTRAIT;
            if (isPortrait) {
//                floatVideoUtils.getPlayerView().setForbidGestureScroll(true);
                floatVideoUtilss.setScrolledY(getCurrentScrollY());
                floatVideoUtilss.onChangeToPortrait();
//                onChangedToPortrait(newConfig);
            } else {
//                onChangedToLandscape(newConfig);
                floatVideoUtilss.onChangeToPortraitLandscape();
//                floatVideoUtils.getPlayerView().setForbidGestureScroll(false);
            }
//            floatVideoUtils.onConfigurationChangedEnd(newConfig);
        }
        if (videoPlayer != null && isFront) {
            videoPlayer.onConfigurationChanged(getActivity(), newConfig, orientationUtils2);
        }
    }

    protected int getCurrentScrollY() {
        return 0;
    }

    protected void onChangedToPortrait(Configuration newConfig) {
    }

    protected void onChangedToLandscape(Configuration newConfig) {
        if (videoPlayer != null) {
//            JLog.e("GSYVideoPlayer  " + videoPlayer.getFullWindowPlayer() + "\n " + videoPlayer);
//            JLog.e("rrsr  " + videoPlayer.getFullWindowPlayer() + "\n " + videoPlayer + "   " + orientationUtils.isClickLand());
        }
    }



    @Override
    public boolean onBackPressed() {
        JLog.e("float VideoUtils " + floatVideoUtilss);
//        if (floatVideoUtils == null || !floatVideoUtils.onBackPress()) {
//            return false;
//        }
//        if (videoPlayer==null || orientationUtils.b)
        if (orientationUtils2 != null) {
            orientationUtils2.backToProtVideo();
        }
        if (StandardGSYVideoPlayer.backFromWindowFull(getContext())) {
            if (orientationUtils2 == null) {
                actionToPortrait();
            }
            return true;
        }
        return super.onBackPressed();
    }

    @Override
    public void onDestroy() {
        if (floatVideoUtilss != null) {
            floatVideoUtilss.onDestory();
        }
        super.onDestroy();
    }

//    public int getCurrentSelectedIndex() {
//        return viewPager.getCurrentItem();
//    }

    BaseDialogFragment.ICallBack netBack = new BaseDialogFragment.ICallBack() {
        @Override
        public void onCallBackMsg(BaseDialogFragment fragment, Message msg) {
            if (msg.what == NO_TAG) {
//                floatVideoUtils.stopVideoView();
            } else if (msg.what == OK_TAG) {
//                floatVideoUtils.setLiving(false).play(img, mVideoUrl);
//                if (topController != null) {
//                    topController.setTitle(titleTemp);
//                }
                videoPlayer.startPlayLogic();
            }
        }
    };


    private void getData() {
        JLog.e("jjaj  will get data  url=" + channelUrl);
        if (TextUtils.isEmpty(channelUrl)) {
            toast(R.string.data_wrong);
            return;
        }
        NetUtils.getNetAdapter().getTvLivingList(getOwnerName(), channelUrl, new AbsNetCallBack<TvLivingList>(TvLivingList.class) {
            @Override
            public void onSuc(TvLivingList tvLivingList) {
                if (tvLivingList != null) {
                    initTvData(tvLivingList);
                }
            }

            @Override
            public void onError(Throwable ex, String errorMsg) {
                super.onError(ex, errorMsg);
            }

            @Override
            public void onEnd() {
                super.onEnd();
                mIsLoading = false;
                refreshLayout.refreshComplete();
            }
        });

    }

    private void initTvData(TvLivingList tvLivingList) {
        List<FChannel> channel = tvLivingList.getChannel();
        mProgramBackUrl = tvLivingList.getLookbackUrl();
        setChannelsData(channel);
//        if (ListUtils.isListValued(channel)) {
//            popTitleView.setVisibility(View.VISIBLE);
//            channels = new ArrayList<>();
//            channels.addAll(channel);
//            currentFChannel = channel.get(0);
//            getTvLivingData(currentFChannel);
//            tvPopTitle.setText(currentFChannel.getChannelName());
//            typedata = new ArrayList<>();
//            for (FChannel fChannel : channel) {
//                typedata.add(fChannel.getChannelName());
//            }
//        } else {
//            popTitleView.setVisibility(View.GONE);
//        }
        // tvAdapter.setParentShowing(isThisShowing);
    }

    private void setChannelsData(List<FChannel> channel) {
        if (ListUtils.isListValued(channel) && currentFChannel == null) {
            currentFChannel = channel.get(0);
            getTvLivingData(currentFChannel);
        }
        if (channelAdapter == null) {
            recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
            recyclerView.setItemAnimator(new DefaultItemAnimator());
            recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
            channelAdapter = new HomeTVChannelAdapter(getContext(), channel);
            if (currentFChannel != null) {
                channelAdapter.setSelectId(currentFChannel.getChannelId(), 0);
            }
            channelAdapter.setItemInternalClick(itemInternalClick);
            recyclerView.setAdapter(channelAdapter);
        } else {
            channelAdapter.setData(channel);
        }
    }

    private void getTvLivingData(FChannel fchannel) {
        if (!TextUtils.isEmpty(fchannel.getLiveUrl())) {
            liveUrl = fchannel.getLiveUrl();

        }
        if (!TextUtils.isEmpty(fchannel.getChannelName())) {
            titleTemp = fchannel.getChannelName();
            liveTitleTemp = fchannel.getChannelName();
        }
        liveImgUrl = fchannel.getDataImg();
        if (topController != null) {
            topController.setTitle(titleTemp);
        }
        setVideoPlayer(liveUrl);
    }


    public void setPlayMedia(FProgram fProgram) {
        String title = fProgram.getProgrammeTitle();
//        tv_title.setText(title);
        if (!TextUtils.isEmpty(fProgram.getProgrammeUrl())) {
            mVideoUrl = UrlUtils.linkUrls(Constances.ST_BASE_URL, fProgram.getProgrammeUrl());
            titleTemp = fProgram.getProgrammeTitle();
//            playVideo();
            if (setVideoPlayer(mVideoUrl)) {
                int netType = NetModeUtil.GetNetype(getContext());
                if (videoPlayer.getThumbImge() != null) {
                    GlideUtil.getGlideWithLarge169DefNotFit(getContext(), fProgram.getProgrammeImage()).into(videoPlayer.getThumbImge());
                }
                JLog.e("bbab  net type=" + netType + "  img=" + fProgram.getProgrammeImage());
                if (netType == 0) {
                    if (floatVideoUtilss != null && videoPlayer != null) {
                        videoPlayer.startPlay();
                    }
                }
            }
        } else {
            // toast("播放地址为空");
        }


    }


    /**
     * 初始化popup窗口
     */
    /**
     * popup窗口里的ListView
     */
    private ListView mTypeLv;
    /**
     * popup窗口
     */
//    private PopupWindow typeSelectPopup;

    /**
     * 数据适配器
     */
    private ArrayAdapter<String> testDataAdapter;

    private List<String> typedata;
    ;

    private void initSelectPopup() {
        View v = LayoutInflater.from(getActivity()).inflate(R.layout.search_popup_list, null);
        mTypeLv = (ListView) v.findViewById(R.id.lv_search_type_popup);
        // 设置适配器
        testDataAdapter = new ArrayAdapter<>(getActivity(), R.layout.item_tv_menu, typedata);
        mTypeLv.setAdapter(testDataAdapter);

        // 设置ListView点击事件监听
        mTypeLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                FChannel tempChannel = channels.get(position);
//                if (tempChannel == null && tempChannel.getChannelId().equals(currentFChannel.getChannelId())) {
//                    typeSelectPopup.dismiss();
//                    return;
//                }
//                // 在这里获取item数据
//                String value = typedata.get(position);
//                // 把选择的数据展示对应的TextView上
//                tvPopTitle.setText(value);
//                if (ListUtils.isListValued(channels)) {
//                    currentFChannel = tempChannel;
//                    getTvLivingData(tempChannel);
//                }
//                // 选择完后关闭popup窗口
//                typeSelectPopup.dismiss();
            }
        });
//        typeSelectPopup = new PopupWindow(v, popTitleView.getWidth(), ActionBar.LayoutParams.WRAP_CONTENT, true);
//        // 取得popup窗口的背景图片
//        Drawable drawable = ContextCompat.getDrawable(getActivity(), R.color.white);
//        typeSelectPopup.setBackgroundDrawable(drawable);
//        typeSelectPopup.setFocusable(true);
//        typeSelectPopup.setOutsideTouchable(true);
//        typeSelectPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
//            @Override
//            public void onDismiss() {
//                // 关闭popup窗口
//                vShape.setVisibility(View.GONE);
//                typeSelectPopup.dismiss();
////                vArrowDown.startAnimation(rotate);
//                vArrowDown.animate().setDuration(200).rotation(0).start();
//            }
//        });
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha 屏幕透明度0.0-1.0 1表示完全不透明
     */
    public void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getActivity().getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        getActivity().getWindow().setAttributes(lp);
    }

}
